﻿using Nasty.Core.Entity.Flow;
using Nasty.Core.Entity.System;
using Nasty.Core.Session;
using SqlSugar;

namespace Nasty.Core.Entity
{
    /// <summary>
    /// 流程实体基类
    /// </summary>
    public abstract class FlowEntity<T> : StandardEntity<T> where T : class, IBaseEntity, new()
    {
        /// <summary>
        /// 流程
        /// </summary>
        [Navigate(NavigateType.OneToOne, nameof(Id), nameof(Flow.Id))]
        public Nasty.Core.Entity.Flow.Flow? Flow { get; set; }

        ///<summary>
        ///流程状态名称
        ///</summary>
        [SugarColumn(ColumnName = "FlowStatus", ColumnDescription = "流程状态名称", IsNullable = true)]
        public string? FlowStatusName { get; set; }

        ///<summary>
        ///流程状态Code
        ///</summary>
        [SugarColumn(ColumnName = "FlowStatusCode", ColumnDescription = "流程状态Code", IsNullable = true)]
        public string? FlowStatusCode { get; set; }


        /////<summary>
        /////流程节点Id（不是配置流程节点Id）
        /////</summary>
        //[SugarColumn(ColumnName = "FlowNodeId", ColumnDescription = "流程节点Id", IsNullable = true)]

        //public string? FlowNodeId { get; set; }

        ///// <summary>
        ///// 流程节点
        ///// </summary>
        //[Navigate(NavigateType.OneToOne, nameof(FlowNodeId), nameof(FlowNode.Id))]
        //public FlowNode? FlowNode { get; set; }



        /// <summary>
        /// 流程节点
        /// </summary>
        [SugarColumn(IsIgnore = true)]
        public FlowNode? FlowNode
        {
            get
            {
                return this.Flow?.CurrentNode;
            }
        }


        ///<summary>
        ///流程节点Name
        ///</summary>
        [SugarColumn(IsIgnore = true)]
        public string? FlowNodeName
        {
            get
            {
                return FlowNode?.Name ?? string.Empty;
            }
        }

        ///<summary>
        ///流程节点Code
        ///</summary>
        [SugarColumn(IsIgnore = true)]
        public string? FlowNodeCode
        {
            get
            {
                return FlowNode?.Code ?? string.Empty;
            }
        }

        ///<summary>
        ///流程是否结束
        ///</summary>
        [SugarColumn(IsIgnore = true)]
        public bool IsFlowEnd
        {
            get
            {
                return this.Flow?.IsEnd ?? false;
            }
        }

        public override void OnPreAdd()
        {
            base.OnPreAdd();
            this.FlowStatusCode = FlowStatus.Wait.Code;
            this.FlowStatusName = FlowStatus.Wait.Name;
        }
    }



    public static class FlowStatus
    {
        public readonly static (string Name, string Code) Wait = ("待启动", "FlowWait");
        public readonly static (string Name, string Code) Start = ("已启动", "FlowStart");
        public readonly static (string Name, string Code) End = ("已完成", "FlowEnd");
        public readonly static (string Name, string Code) Approval = ("审批中", "FlowApproval");
        public readonly static (string Name, string Code) Reject = ("已驳回", "FlowReject");
        public readonly static (string Name, string Code) Revoke = ("已撤销", "FlowRevoke");
    }
}
